<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\command;

use app\enum\CommonEnum;
use app\model\KsHostingProjectModel;
use app\model\KsProjectClickUrlTaskModel;
use app\service\client\AccountService;
use app\service\kuaishou\AdvertiserService;
use think\console\Input;
use think\console\Output;

class KsProjectClickUrlEditWork extends BaseWork
{
    protected function configure()
    {
        // 指令配置
        $this->setName('ks_project_click_url_edit_work');
        $this->setDescription('修改快手智能托管检测链接脚本');
    }

    protected function execute(Input $input, Output $output)
    {
        set_time_limit(0);
        ini_set('memory_limit', '-1');
        $output->writeln('=======修改快手智能托管检测链接开始' . date('Y-m-d H:i:s', time()) . '=======');
        try {
            $projects = (new KsProjectClickUrlTaskModel())->where('status', 0)->select();
            foreach ($projects as $project) {
                $num = 0;
                $advertiserId = $project['advertiser_id'];
                $projectIds = json_decode($project['project_ids']);
                foreach ($projectIds as $projectId) {
                    $token = AccountService::getToken(CommonEnum::KS_MEDIA, $advertiserId);
                    if (! $token) {
                        echo 'token获取失败';
                        exit;
                    }
                    $headers = [
                        'Access-Token' => $token,
                        'Content-Type' => 'application/json',
                    ];
                    $params = [
                        'advertiser_id' => $advertiserId,
                        'project_id' => $projectId,
                    ];
                    $result = AdvertiserService::getHostingProjectDetail($headers, $params);
                    if (! $result) {
                        echo $projectId . '： 失败' . self::getError(0) . PHP_EOL;
                        exit;
                    }
                    $detail = $result['data'];
                    // 修改数据
                    if ($detail['click_url'] == $project['url']) {
                        echo $projectId . '无需修改' . PHP_EOL;
                        continue;
                    }
                    $detail['click_url'] = $project['url'];
                    $detail['advertiser_id'] = $advertiserId;
                    unset($detail['campaign_id'], $detail['put_status'], $detail['create_time'], $detail['update_time']);
                    $result = AdvertiserService::updateProject($headers, $detail);
                    if ($result === false) {
                        echo $projectId . '： 失败' . json_encode(AdvertiserService::getError(0), JSON_UNESCAPED_UNICODE) . PHP_EOL;
                        continue;
                    }
                    $res = KsHostingProjectModel::where('advertiser_id', $advertiserId)->where('project_id', $projectId)
                        ->update(['click_url' => $project['url'], 'update_at' => time()]);
                    if ($res) {
                        ++$num;
                        echo $projectId . '： 成功' . PHP_EOL;
                    } else {
                        echo $projectId . '： 修改数据库失败' . PHP_EOL;
                    }
                }
                // 修改状态
                $project->setAttr('status', 1);
                $project->setAttr('num', $num);
                $project->save();
            }
        } catch (\Exception $e) {
            $output->writeln('=======修改快手智能托管检测链接失败' . $e->getMessage() . date('Y-m-d H:i:s', time()) . '=======');
        }
        $output->writeln('=======修改快手智能托管检测链接结束' . date('Y-m-d H:i:s', time()) . '=======');
    }
}
